/*
                    Statbar Class Definition
*/
#include "Statbar.h"

Statbar::Statbar()
{
    iMaxValue = 0;
    fCurrentPercent = 0.0f;
    iCurrentValue = 0;
    iCurrentBar = 0;
    iCurrentWidth = 0;

    Position = Point(0, 0);

    bVisible = true;
}

Statbar::~Statbar()
{
    for (int i = 0; i < Bars.size(); i++)
    {
        delete Bars.at(i);
    }

    Bars.clear();
}

int Statbar::Initialise(Point NewPosition, Bar* pBar, const int& iNewMaxVal, const int& iNewCurVal, const string& strFilepath)
{
    iMaxValue = iNewMaxVal;
    iCurrentValue = iNewCurVal;

    Bars.push_back(pBar);

    Background = OptimiseAlphaSurface(IMG_Load(strFilepath.c_str()));

    Position = NewPosition;

    iCurrentBar = 0;

    CalculateBars();
}

void Statbar::setPosition(Point NewPosition)
{
    Position = NewPosition;
}

Point Statbar::getPosition()
{
    return Position;
}

int Statbar::AddBar(Bar* pNew)
{
    Bars.push_back(pNew);
}

void Statbar::setCurrentValue(const int& iValue)
{
    iCurrentValue = iValue;

    CalculateBars();
}

int Statbar::getCurrentValue()
{
    return iCurrentValue;
}

void Statbar::setMaxValue(const int& iValue)
{
    iMaxValue = iValue;

    CalculateBars();
}

int Statbar::getMaxValue()
{
    return iMaxValue;
}

void Statbar::OffsetValue(const int& iAmount)
{
    iCurrentValue += iAmount;

    CalculateBars();
}

void Statbar::CalculateBars()
{
    if (iCurrentValue > iMaxValue)
    {
        iCurrentValue = iMaxValue;
    }
    else if (iCurrentValue < 0)
    {
        iCurrentValue = 0;
    }

    fCurrentPercent = ((float) iCurrentValue / (float)iMaxValue) * 100.0f;

    float fTemp = (fCurrentPercent / 100.0f) * static_cast<float>(Bars.at(iCurrentBar)->Image->w);

    if (fTemp > 0.0f && fTemp < 1.0f) { fTemp = 1.0f; }

    iCurrentWidth = static_cast<int>(fTemp);
}

int Statbar::BlitToSurface(SDL_Surface *screen)
{
    if (bVisible == true)
    {
        if (Background == NULL)
        {
            return -1;
        }

        else
        {
            SDL_Rect dest, src;
            dest.x = static_cast<int>(Position.x);
            dest.y = static_cast<int>(Position.y);
            SDL_BlitSurface(Background, NULL, screen, &dest);

            if (iCurrentBar < Bars.size())
            {
                dest.x = static_cast<int>(Position.x) + static_cast<int>(Bars.at(iCurrentBar)->Position.x);
                dest.y = static_cast<int>(Position.y) + static_cast<int>(Bars.at(iCurrentBar)->Position.y);
                src.x = 0;
                src.y = 0;
                src.h = Bars.at(iCurrentBar)->Image->h;
                src.w = iCurrentWidth;
                SDL_BlitSurface(Bars.at(iCurrentBar)->Image, &src, screen, &dest);
            }

            return 0;
        }
    }
}

int Statbar::BlitToFrame(SDL_Surface *surface, Point FramePosition)
{
    if (bVisible == true)
    {
        if (Background == NULL)
        {
            return -1;
        }
        else
        {
            SDL_Rect dest, src;
            dest.x = static_cast<int>(Position.x) + static_cast<int>(FramePosition.x);
            dest.y = static_cast<int>(Position.y) + static_cast<int>(FramePosition.y);
            SDL_BlitSurface(Background, NULL, surface, &dest);

            if (iCurrentBar < Bars.size())
            {
                dest.x = static_cast<int>(Position.x) + static_cast<int>(FramePosition.x) + static_cast<int>(Bars.at(iCurrentBar)->Position.x);
                dest.y = static_cast<int>(Position.y) + static_cast<int>(FramePosition.y) + static_cast<int>(Bars.at(iCurrentBar)->Position.y);
                src.x = 0;
                src.y = 0;
                src.h = Bars.at(iCurrentBar)->Image->h;
                src.w = iCurrentWidth;
                SDL_BlitSurface(Bars.at(iCurrentBar)->Image, &src, surface, &dest);
            }

            return 0;
        }
    }
}
